"""
@author: wg
@file: download.py
@time: 2023/11/30 10:42
"""


# 把可重复使用的功能进行封装
# download模块（文件名：符合标识符的命名规则）
import time
import requests
import logging
# 获取文本
def downloadtext(url):
    for i in range(1):
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
        }
        try:
            # try内部写有可能会出错的代码
            # 获取网页数据
            # 返回一个response对象
            response = requests.get(url, headers=headers, verify=False)
            print(response)
            response.raise_for_status()
            # 设置编码
            response.encoding = response.apparent_encoding
            time.sleep(3)
            # print(url, "访问成功")
            logging.debug(url, "访问成功")
            # 返回文本数据
            return response.text
        except:
            # 如果出错了会执行这个
            # print(url, "访问错误")
            logging.debug(url, "访问成功")
            return ""

# 获取二进制
def downloadcontent(url):
    for i in range(1):
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
        }
        try:
            # try内部写有可能会出错的代码
            # 获取网页数据
            # 返回一个response对象
            response = requests.get(url, headers=headers, verify=False)
            response.raise_for_status()
            # 设置编码
            response.encoding = response.apparent_encoding
            time.sleep(3)
            print(url, "访问成功")
            # 返回文本数据
            return response.content
        except:
            # 如果出错了会执行这个
            print(url, "访问错误")
            return ""
